<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class TagController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
		$data = DB::table('tags')->orderBy('weight', 'desc')->get();
		return view('admin/tag/index')->with('data', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
		return view('admin/tag/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
		// 表单验证
		$this->validate($request, [
		    'name' => 'required|regex:/.{3,20}/|unique:tags',
		],[
		    'name.required' => '标签不能为空',
		    'name.regex' => '标签长度为3-20位',
		    'name.unique' => '标签不能重复',
		]);
		     
		$data = $request->except('_token');
		$data['weight'] = empty($data['weight']) ? 0 : $data['weight'];
		if (DB::table('tags')->insert($data)) {
		    return redirect('/admin/tag/index');
		} else {
		    return redirect('/admin/tag/edit/'.$data['id']);
		}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
		$olddata = DB::table('tags')->find($id);
		return view('admin/tag/edit')->with('olddata', $olddata);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
		// 表单验证
		$rule = new \Illuminate\Validation\Rule;
		$this->validate($request, [
		    'name' => 'required|regex:/\w{3,20}/',
			'name' => $rule->unique('tags')->ignore($id),
		],[
		    'name.required' => '标签不能为空',
		    'name.regex' => '标签长度为3-20位',
		    'name.unique' => '标签不能重复',
		]);
		     
		$data = $request->except('_token');
		$data['weight'] = empty($data['weight']) ? 0 : $data['weight'];
		if (DB::table('tags')->where('id', '=', $id)->update($data)) {
		    return redirect('/admin/tag/index');
		} else {
		    return redirect('/admin/tag/edit/'.$data['id']);
		}
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
		DB::table('tags')->where('id', '=', $id)->delete();
		return redirect('/admin/tag/index');
    }
	
	/**
	 * 批量删除
	 */
	public function dels(Request $request)
	{
		if($ids = $request->input('ids')){
			DB::table('tags')->whereIn('id', $ids)->delete();
		}
		return redirect('/admin/tag/index');
	}
}
